<template>
  <div>
    <el-row class="line-bottom" v-show="showSearch">
      <el-col class="form_col">
        <el-form :model="queryParams" ref="queryForm" key="terminalHistoryQueryForm" :size="formItemSize"
                 v-show="showSearch" :label-width="labelWidth">
          <div style="display: flex; justify-content: start; flex-wrap: wrap;">
            <el-form-item label="抄表时间" prop="freezeTimeRange">
              <el-date-picker :size="formItemSize"
                              v-model="freezeTimeRange"
                              type="datetimerange"
                              value-format="yyyy-MM-dd HH:mm:ss"
                              style="width: 320px;"
                              :clearable="false"
                              range-separator="至"
                              start-placeholder="开始日期"
                              end-placeholder="结束日期"
                              :default-time="['00:00:00', '23:59:59']">
              </el-date-picker>
            </el-form-item>
          </div>
        </el-form>
      </el-col>

      <el-col class="form_query">
        <el-button type="primary" @click="handleQuery" size="mini">查询</el-button>
        <el-button @click="resetQuery" size="mini">重置</el-button>
      </el-col>
    </el-row>

    <el-row :gutter="10" class="mb8">
      <RightDropdown :dayColumns="historyColumns"
                     :showSearch.sync="showSearch" @queryTable="getList"
                     @export="handleExport"></RightDropdown>
    </el-row>

    <el-row>
      <el-table ref="table" v-loading="loading" :data="historyList" border size="mini" :height="getAvailableHeight">
        <el-table-column label="#" type="index" align="center" width="40"/>
        <el-table-column label="抄表时间" align="center" prop="readDate" v-if="historyColumns[0].visible"
                         :show-overflow-tooltip="true" width="180"/>
        <el-table-column label="总流量(m³)" align="center" prop="totalFlow" v-if="historyColumns[1].visible"
                         :show-overflow-tooltip="true"/>
        <el-table-column label="正向读数(m³)" align="center" prop="readNumber" v-if="historyColumns[2].visible"
                         :show-overflow-tooltip="true"/>
        <el-table-column label="反向读数(m³)" align="center" prop="reverseNumber" v-if="historyColumns[3].visible"
                         :show-overflow-tooltip="true"/>
        <el-table-column label="瞬时流量(m³/h)" align="center" prop="instantNumber" v-if="historyColumns[4].visible"
                         :show-overflow-tooltip="true"/>
        <!-- <el-table-column label="瞬时流速(m³/h)" align="center" prop="InstantVelocity" v-if="columns[5].visible" :show-overflow-tooltip="true" width="160"/> -->
        <el-table-column label="管网压力(MPA)" align="center" prop="pressure" v-if="historyColumns[5].visible"
                         :show-overflow-tooltip="true"/>
        <el-table-column label="终端电压(V)" align="center" prop="terminalVol" v-if="historyColumns[6].visible"
                         :show-overflow-tooltip="true"/>
        <el-table-column label="终端信号强度(dBm)" align="center" prop="terminalCsq" v-if="historyColumns[7].visible"
                         :show-overflow-tooltip="true"/>
        <!-- <el-table-column label="信号描述" align="center" prop="TerminalCSQDescribe" v-if="false" :show-overflow-tooltip="true"/> -->
        <el-table-column label="阀门状态" align="center" prop="valveState" v-if="historyColumns[8].visible"
                         :show-overflow-tooltip="true">
          <template slot-scope="scope">
            <span v-if="scope.row.ValveState===0">关</span>
            <span v-if="scope.row.ValveState===1">开</span>
          </template>
        </el-table-column>
      </el-table>
      <pagination
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
      />
    </el-row>

  </div>
</template>

<script>
import moment from "moment/moment";
import {listData} from "@/api/dataquery/terminal/terminalhistory.js";

export default {
  name: "History",
  data() {
    return {
      screenHeight: null,
      loading: false,
      // 显示搜索条件
      showSearch: true,
      collapseAble: false,
      formItemSize: 'mini',
      maxShow: 4,
      labelWidth: '80px',
      // 总条数
      total: 0,
      // 表格数据
      historyList: [],
      // 列信息
      historyColumns: [
        {key: 0, label: `抄表时间`, visible: true},
        {key: 1, label: `总流量`, visible: true},
        {key: 2, label: `正向读数`, visible: true},
        {key: 3, label: `反向读数`, visible: true},
        {key: 4, label: `瞬时流量`, visible: true},
        // { key: 5, label: `瞬时流速`, visible: true },
        {key: 5, label: `管网压力`, visible: true},
        {key: 6, label: `终端电压`, visible: true},
        {key: 7, label: `终端信号强度`, visible: true},
        {key: 8, label: `阀门状态`, visible: false},
      ],
      freezeTimeRange: [moment().subtract(1, 'day').format('YYYY-MM-DD 00:00:00'), moment().subtract(1, 'day').format('YYYY-MM-DD 23:59:59')],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 20,
        terminalId: null,
        params: {
          startTime: moment(new Date()).format('YYYY-MM-DD 00:00:00'),
          endTime: moment(new Date()).format('YYYY-MM-DD 23:59:59')
        },
      },
    }
  },
  created() {
    this.screenHeight = document.body.clientHeight
  },
  computed: {
    getAvailableHeight() {
      return this.screenHeight - (this.showSearch ? (this.fold ? 295 : 255) : 210)
    }
  },
  methods: {
    updateTerminalId(terminalId) {
      this.queryParams.terminalId = terminalId
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList()
    },
    // 重置表单
    resetQuery() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 20,
        command: null,
        terminalId: null,
        sendTime: null,
      }
      this.handleQuery()
    },
    // 初始化表格 解决表格固定操作列的问题
    init() {
      // 调用接口获取到数据后
      let _self = this
      this.$nextTick(() => {
        window.setTimeout(() => {
          _self.$refs.table.doLayout()
        }, 100)
      })
    },
    // 获取表格数据
    getList() {
      this.loading = true;
      if (this.freezeTimeRange.length > 0) {
        this.queryParams.params.startTime = this.freezeTimeRange[0]
        this.queryParams.params.endTime = this.freezeTimeRange[1]
      }

      listData(this.queryParams).then(response => {
        this.historyList = response.rows;
        this.total = response.total;
        this.init()
        this.loading = false;
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('datastatistics/terminalhistory/export', {
        ...this.queryParams,
      }, `大用户历史统计数据.xlsx`)
    }
  }

}
</script>

<style scoped lang="scss">
.form_col {
  width: 80%;

  ::v-deep .el-form {
    width: 100%;
  }

  ::v-deep .el-form-item {
    width: 20%;
  }
}

.form_query {
  width: 20%;
  text-align: right;
}
</style>
